package com.keiou.drink.util;

import android.text.TextUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Bob on 2016/7/8
 */
public class TimeUtil {


    /**
     * 毫秒转时间格式
     *
     * @param milliseconds
     * @return
     */
    public static String getDate(String milliseconds) {
        String dataStr = "";
        if (!TextUtils.isEmpty(milliseconds)) {
            try {
                Date d = new Date(Long.valueOf(milliseconds));
                SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                dataStr = format.format(d);
            } catch (Exception e) {
                dataStr = "Unknown";
            }
        }
        return dataStr;
    }


    /**
     * @param milliseconds
     * @return
     */
    public static String getDate(long milliseconds) {
        String dataStr = "Unknown";
        if (milliseconds > 0) {
            try {
                Date d = new Date(Long.valueOf(milliseconds));
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                dataStr = format.format(d);
            } catch (Exception e) {
                dataStr = "Unknown";
            }
        }
        return dataStr;
    }


    public static String getDate(long milliseconds, String formatStr) {
        String dataStr = "Unknown";
        if (milliseconds > 0) {
            try {
                Date d = new Date(Long.valueOf(milliseconds));
                SimpleDateFormat format = new SimpleDateFormat(formatStr);
                dataStr = format.format(d);
            } catch (Exception e) {
                dataStr = "Unknown";
            }
        }
        return dataStr;
    }

    public static String getDate(String str, String formatStr) {
        String dataStr = "Unknown";
        try {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);
            Date strtodate = format.parse(str);
            dataStr = format.format(strtodate);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return dataStr;
    }


    /**
     * 根据时间获取天数
     *
     * @param dateStr
     * @param format
     * @return
     * @throws ParseException
     */
    public static long getDay(String dateStr, String format) throws ParseException {
        Date date = new SimpleDateFormat(format).parse(dateStr);
        return getDay(date.getTime());
    }

    public static long getDay(long milliseconds) {
        return (long) Math.ceil(milliseconds / (24f * 60 * 60 * 1000));
    }


    /**
     * 根据天数获取时间
     *
     * @param day
     * @return
     */
    public static String getDay(int day) {
        Date date = new Date();
        date.setTime((long) day * 24 * 60 * 60 * 1000);
        SimpleDateFormat myFmt = new SimpleDateFormat("yyyy-MM-dd");
        return myFmt.format(date);
    }


}
